package com.woniuxy.threadmethods;

import lombok.extern.slf4j.Slf4j;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/4/6:19:03
 * <P> Description:
 */
@Slf4j(topic = "c.Test13")
public class Test13 {
    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        TwoPhaseTermination1 tpt = new TwoPhaseTermination1();
        tpt.start();

        Thread.sleep(3500);
        tpt.stop();
    }
}

@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination {
    private Thread monitor;

    /**
     * TODO:启动监控现场
     **/
    public void start() {
        monitor = new Thread(() -> {
            while (true) {
                Thread currentThread = Thread.currentThread();
                if (currentThread.isInterrupted()) {
                    log.debug("被打断，结束循环");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    log.debug("执行监控");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //TODO 重新设置打断标记
                    currentThread.interrupt();
                }
            }
        });

        monitor.start();
    }

    public void stop() {
        if (monitor != null) {
            monitor.interrupt();
        }
    }
}
